package com.app.shop.service.system.impl;

import com.app.shop.util.SysModel;
import com.app.shop.dao.system.SysReAuthUserMapper;
import com.app.shop.entity.SysReAuthUser;
import com.app.shop.entity.SysReAuthUserExample;
import com.app.shop.entity.query.SysUserQuery;
import com.app.shop.service.system.SysReAuthUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 权限服务
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("sysReAuthUserService")
public class SysReAuthUserServiceImp implements SysReAuthUserService {

    private static final Logger log = LoggerFactory.getLogger(SysReAuthUserServiceImp.class);

    @Autowired
    private SysReAuthUserMapper sysReAuthUserMapper;

    @Override
    public List<SysReAuthUser> getSysAuthListByUserId(Integer userId) {
        SysReAuthUserExample sysReAuthUserExample = new SysReAuthUserExample();
        SysReAuthUserExample.Criteria criteria = sysReAuthUserExample.createCriteria();
        criteria.andStatusEqualTo(1).andUserIdEqualTo(userId);
        return sysReAuthUserMapper.selectByExample(sysReAuthUserExample);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel updateAuthUserInfo(SysUserQuery sysUserQuery) {
        SysModel sysModel = new SysModel();
        try {
            if(!sysUserQuery.getUserId().equals(1001)) {
                SysReAuthUserExample sysReAuthUserExample = new SysReAuthUserExample();
                SysReAuthUserExample.Criteria criteria = sysReAuthUserExample.createCriteria();
                criteria.andUserIdEqualTo(sysUserQuery.getUserId()).andStatusEqualTo(1);
                SysReAuthUser sysReAuthUser = new SysReAuthUser();
                sysReAuthUser.setVersion(new Date());
                sysReAuthUser.setStatus(-1);
                sysReAuthUser.setUpdateUserId(sysUserQuery.getOptionUserId());

                if (sysUserQuery.getAuthIds() != null && sysUserQuery.getAuthIds().length > 0) {
                    List<Integer> authList = new ArrayList<Integer>();
                    authList.addAll(Arrays.asList(sysUserQuery.getAuthIds()));
                    criteria.andAuthIdNotIn(authList);
                    //将当前用户权限失效
                    sysReAuthUserMapper.updateByExampleSelective(sysReAuthUser, sysReAuthUserExample);
                    //查询已经权限
                    sysReAuthUserExample = new SysReAuthUserExample();
                    SysReAuthUserExample.Criteria criteriaIn = sysReAuthUserExample.createCriteria();
                    criteriaIn.andUserIdEqualTo(sysUserQuery.getUserId()).andAuthIdIn(authList).andStatusEqualTo(1);
                    List<SysReAuthUser> list = sysReAuthUserMapper.selectByExample(sysReAuthUserExample);
                    //系统已经存在权限不进行循环插入
                    boolean bool;
                    //开始循环添加权限
                    for (Integer it : sysUserQuery.getAuthIds()) {
                        bool = true;
                        for (SysReAuthUser srau : list) {
                            if (srau.getAuthId().equals(it)) {
                                bool = false;
                                break;
                            }
                        }
                        if (bool) {
                            //增加生效规则，增加批量插入sql
                            sysReAuthUser = new SysReAuthUser();
                            sysReAuthUser.setCreateDate(new Date());
                            sysReAuthUser.setStatus(1);
                            sysReAuthUser.setCreateUserId(sysUserQuery.getOptionUserId());
                            sysReAuthUser.setVersion(new Date());
                            sysReAuthUser.setUserId(sysUserQuery.getUserId());
                            sysReAuthUser.setAuthId(it);
                            sysReAuthUser.setUpdateUserId(sysUserQuery.getOptionUserId());
                            sysReAuthUserMapper.insert(sysReAuthUser);
                        }
                    }
                } else {
                    //将当前用户权限失效
                    sysReAuthUserMapper.updateByExampleSelective(sysReAuthUser, sysReAuthUserExample);
                }
                sysModel.setSuccess(true);
            }else{
                sysModel.setSuccess(false);
                sysModel.setMsg("超级管理员不允许变更角色！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("用户角色修改过程发生异常,请重试或联系管理员！");
            log.error("用户角色修改过程发生异常：" + e);
        }
        return sysModel;
    }
}
